package es.deusto.eside.programacion4.mario2d.states;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.tiled.TiledMap;
import org.newdawn.slick.util.Log;

import es.deusto.eside.programacion4.mario2d.Mario2D;
import es.deusto.eside.programacion4.mario2d.model.BasicCharacter;
import es.deusto.eside.programacion4.mario2d.model.FinalBoss;
import es.deusto.eside.programacion4.mario2d.model.Mario;
import es.deusto.eside.programacion4.mario2d.model.Proyectile;
import es.deusto.eside.programacion4.mario2d.utils.Camera;

public class FinalState extends StageModel {

	FinalBoss bowser;
	int finalLifeMario;
	Body bBoss;
	/**
	 * Definicion del cuerpo
	 */
	BodyDef bDBoss;
	/**
	 * Tipo de cuerpo
	 */
	PolygonShape dBoss;
	/**
	 * Textura del cuerpo
	 */
	FixtureDef fBoss;

	Body bossFire;
	/**
	 * Definicion del cuerpo
	 */
	BodyDef bBFire;
	/**
	 * Tipo de cuerpo
	 */
	CircleShape bFireBox;
	/**
	 * Textura del cuerpo
	 */
	FixtureDef bossFireDef;

	int timerCanShoot = 0;
	int timer = 0;
	private Proyectile pB;

	/**
	 * Constructor
	 * 
	 * @param ordinal
	 */
	public FinalState(int ordinal) {
		super(ordinal);
	}

	@Override
	public void init(GameContainer gC, StateBasedGame sb) {
		super.init(gC, sb);
		finalLifeMario = 2;
		try {
			bowser = new FinalBoss(500, 368);
		} catch (SlickException e) {
			e.printStackTrace();
		}
		Log.debug(bowser.getPositionX() + " " + bowser.getPositionY());

		// Bowser
		pB = new Proyectile(bowser.getPositionX() - 20,
				bowser.getPositionY() + 10);
		bDBoss = new BodyDef();
		bDBoss.type = BodyType.STATIC;
		bDBoss.position.set(bowser.getPositionX() + 49 / 2,
				bowser.getPositionY() + 56 / 2);

		bBoss = world.createBody(bDBoss);
		dBoss = new PolygonShape();
		dBoss.setAsBox(49 / 2, 56 / 2);
		fBoss = new FixtureDef();
		fBoss.shape = dBoss;
		fBoss.density = 1;
		fBoss.friction = 0.3f;
		bBoss.createFixture(fBoss);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta) {
		super.update(gc, sb, delta);
		Log.error("mario " + mario.getPositionY());
		timerCanShoot += delta;
		System.out.println("posicion x: " + mario.getPositionX());
		camera.centerOn(mario.getPositionX(), mario.getPositionY());
		world.step(timeStep, velocityIterations, positionIterations);
		mario.setPositionX(bPlayer.getPosition().x);
		mario.setPositionY(bPlayer.getPosition().y);
		System.out.println("posicion y: " + mario.getPositionY());
		// bowser fire
		// body definition
		bBFire = new BodyDef();
		bBFire.position.set(pB.getPositionx() + 5, pB.getPositionY() + 5);
		bBFire.type = BodyType.DYNAMIC;

		// define shape of the body.
		bFireBox = new CircleShape();
		bFireBox.m_radius = 0.5f;

		// define fixture of the body.
		fireDef = new FixtureDef();
		fireDef.shape = bFireBox;
		fireDef.density = 0.5f;
		fireDef.friction = 0.3f;
		fireDef.restitution = 0.5f;
		// create the body and add fixture to it
		bossFire = world.createBody(bDBoss);
		bossFire.createFixture(fireDef);
		if (timerCanShoot >= 1000) {
			timer += 1;
			timerCanShoot = 0;
		}
		if (bowser.isDead() == false && timer == 0) {
			pB.setCanBeShooted(false);
			pB.setItShooted(true);
			bossFire.m_linearVelocity.x = -1000;
			pB.setPositionx((int) bossFire.getPosition().x);
			pB.setPositionY(400);

		} else if (bowser.isDead() == false && timer > 2) {
			pB.setCanBeShooted(true);
			pB.setItShooted(false);
			timer = 0;
		} else if (timer < 4) {
			pB.setPositionx(pB.getPositionx() - 5);
		}

		if (pB.getPositionx() - p.getPositionx() < 5
				&& pB.getPositionx() - p.getPositionx() > 0) {
			p.setItShooted(false);
			p.setCanBeShooted(true);
		}
		if (bowser.getPositionX() - p.getPositionx() < 5
				&& p.isItShooted() == true) {
			bowser.setLife(bowser.getLife() - 1);
			Log.error("vida bowser " + bowser.getLife());
			p.setItShooted(false);
			p.setCanBeShooted(true);
		}
		if (bowser.getLife() == 0) {
			bowser.setDead(true);
			bBoss.setActive(false);
			Mario2D.points += 1000;
			Mario2D.points += (120000 - timer) / 100;
		}
		if (pB.getPositionY() - mario.getPositionY() <= 36
				&& pB.getPositionx() - mario.getPositionX() < 5
				&& pB.getPositionx() - mario.getPositionX() >= 0) {
			finalLifeMario--;
			Log.error("vidas de este nivel: " + finalLifeMario);
		}
		if (finalLifeMario == 0) {
			Mario2D.lifes = Mario2D.lifes - 1;
			mario.setDead(true);
		}
		if (bowser.getLife() == 0) {
			bowser.setDead(true);
			bBoss.setActive(false);
			Mario2D.points += 1000;
			Mario2D.points += (120000 - timer) / 100;
			init(gc, sb);
			sb.enterState(GameState.RANKING.ordinal(), new FadeOutTransition(),
					new FadeInTransition());

		}
		if (timer > 120000) {
			init(gc, sb);

			sb.enterState(GameState.RANKING.ordinal(), new FadeOutTransition(),
					new FadeInTransition());
		}
	}

	@Override
	public void initTile(GameContainer gC) throws SlickException {
		Log.debug("Enemigo final");
		mario = new Mario();
		mario.setBig(true);
		mario.setFire(true);
		mario.setPositionX(50);
		mario.setPositionY(380);
		mario.setCurrent(mario.getWalkFire());
		camera = new Camera(gC, new TiledMap(
				"resources/Tile/FinalLevel/FinalLevel/FinalLevel.tmx"));
		solid = new int[24][75];
		for (int i = 0; i < 24; i++) {
			for (int j = 0; j < 32; j++) {
				// bloque
				if (camera.getMap().getTileId(j, i, 0) != 0) {
					solid[i][j] = 1;
				}
				// enemigo
				else if (camera.getMap().getTileId(j, i, 1) != 0) {
					solid[i][j] = 2;
				} else {
					solid[i][j] = 0;
				}

			}
		}
		for (int i = 0; i < 24; i++) {
			for (int j = 0; j < 75; j++) {
				System.out.print(solid[i][j]);

			}
			System.out.println("");
		}
	}

	@Override
	public void initEnemies() {

	}

	@Override
	public void drawEnemies(Graphics g) {

	}

	@Override
	public void render(GameContainer gc, StateBasedGame sb, Graphics g) {
		super.render(gc, sb, g);
		if (bowser.isDead() == false) {
			bowser.getCurrent().draw(bowser.getPositionX(),
					bowser.getPositionY());
			bBoss.setActive(false);
		}
		if (pB.isItShooted() == true) {
			pB.getProyectile().draw(pB.getPositionx(), pB.getPositionY());
		}
	}
}
